home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Development Tools & Languages / Bits o' MacApp Code / Text, Grid, List Views / ProtoViews.cp next >
Encoding:
Text File  |  1993-06-10  |  7.4 KB  |  250 lines  |  [TEXT/MPS ]

  1. #ifndef __MACAPP__
  2. #include <MacApp.h>
  3. #endif
  4.  
  5. #ifndef __FONTS__
  6. #include <Fonts.h>
  7. #endif
  8.  
  9. #ifndef __PROTOVIEWS__
  10. #include "ProtoViews.h"
  11. #endif
  12.  
  13. //------------------------------ •TLineData• ---------------------------------
  14. // Basic data element class used by the "TLineServer" to deliver data to the view
  15.  
  16. //----------------------------------------------------------------------------------------
  17. // TLineData::ILineData: 
  18. //----------------------------------------------------------------------------------------
  19. #pragma segment MAOpen
  20. pascal void TLineData::ILineData(const long offset, 
  21.                                       TFile *aFile)
  22. {
  23.     fOffset = offset;
  24.     fMyFile = aFile;
  25. }
  26.  
  27. //----------------------------------------------------------------------------------------
  28. // TLineData::GetText: 
  29. //----------------------------------------------------------------------------------------
  30. #pragma segment GVRes
  31. pascal void TLineData::GetText(CStr255 &theText)
  32. {
  33.     char    theBuffer[256];
  34.     long     count = 255;
  35.     char    cr = 13;
  36.     
  37.     fMyFile->SetDataMark(fOffset, fsFromStart);
  38.     fMyFile->ReadUntil(theBuffer, count, cr);
  39.     theBuffer[count] = 0;
  40.     theText = theBuffer;
  41. }
  42.  
  43.  
  44. //------------------------------ •TLineServer• ---------------------------------
  45. // TLineServer reads a CR delimited text file and creates an instance of TLineData for
  46. // each line it finds. 
  47.  
  48. //----------------------------------------------------------------------------------------
  49. // TLineServer::ILineServer: 
  50. //----------------------------------------------------------------------------------------
  51. #pragma segment MAOpen
  52. pascal void TLineServer::ILineServer(CStr255 &fileName)
  53. {
  54.     ProcessSerialNumber    psn;
  55.     FSSpec                 fileSpec;
  56.     char    theBuffer[256];
  57.     long    count;
  58.     char    cr = 13;
  59.     
  60.     fLineList = NewList();
  61.     
  62.     // NOTE: assumption is that the text file lives in the same folder as the application.
  63.     // Also note conspicuous lack of error checking
  64.     // Hey, gimme a break, this was written for prototype
  65.     
  66.     OSErr theErr = FindProcessBySignature(gApplication->fCreator, psn, &fileSpec);
  67.     
  68.     // Note text file creator - If you're reading this you must have MPW
  69.     
  70.     fMyFile = NewFile('TEXT', 'MPS ', kUsesDataFork, kUsesRsrcFork, true, false);
  71.     fMyFile->SpecifyWithTrio(fileSpec.vRefNum, fileSpec.parID, fileName);
  72.     
  73.     fMyFile->OpenFile();
  74.     
  75.     OSErr err = noErr;
  76.     long offset = 0;
  77.     
  78.     do {
  79.         count = 256;
  80.         err = fMyFile->ReadUntil(theBuffer, count, cr);
  81.         if(err == noErr) {
  82.             TLineData *aLine = new TLineData;
  83.             aLine->ILineData(offset, fMyFile);
  84.             fLineList->InsertLast(aLine);
  85.             fMyFile->GetDataMark(offset);
  86.         }
  87.     } while(err == noErr);
  88. }
  89.  
  90. //----------------------------------------------------------------------------------------
  91. // TLineServer::GetALine: 
  92. //----------------------------------------------------------------------------------------
  93. #pragma segment GVRes
  94. pascal void TLineServer::GetALine(const ArrayIndex theLine,
  95.                                      CStr255 &theText)
  96. {
  97.     TLineData *aLine = (TLineData *)fLineList->At(theLine);
  98.     aLine->GetText(theText);
  99. }
  100.  
  101. //----------------------------------------------------------------------------------------
  102. // TLineServer::GetLineCount: 
  103. //----------------------------------------------------------------------------------------
  104. #pragma segment MAOpen
  105. pascal ArrayIndex TLineServer::GetLineCount()
  106. {
  107.     return fLineList->GetSize();
  108. }
  109.  
  110. //----------------------------------------------------------------------------------------
  111. // TLineServer::Free: 
  112. //----------------------------------------------------------------------------------------
  113. #pragma segment MAClose
  114. pascal void TLineServer::Free()
  115. {
  116.     fLineList->FreeList();
  117.     fMyFile->Free();
  118.     inherited::Free();
  119. }
  120.  
  121.  
  122. //------------------------------ •TLineView• ---------------------------------
  123. // Basic text display class.
  124. //----------------------------------------------------------------------------------------
  125. // TLineView::ILineView: 
  126. //----------------------------------------------------------------------------------------
  127. #pragma segment MAOpen
  128. pascal void TLineView::ILineView(TLineServer *aLineServer)
  129. {
  130.     VRect myExtent;
  131.     FontInfo    fi;
  132.     VPoint    limit;
  133.     
  134.     fMyLineServer = aLineServer;
  135.     ArrayIndex lineCount = fMyLineServer->GetLineCount();
  136.     
  137.     this->GetExtent(myExtent);    // Get our current size
  138.     this->SetupDrawingEnvironment();
  139.  
  140.     TextFont(times);
  141.     TextSize(12);
  142.     TextFace(normal);
  143.  
  144.     GetFontInfo(fi);
  145.     short lh = fi.ascent+fi.descent+fi.leading;
  146.     
  147.     limit.v = lh;
  148.     limit.h = 0;
  149.     ((TScroller *)fSuperView)->SetScrollParameters(limit,true,true);
  150.     
  151.     myExtent.bottom = myExtent.top + (lh*lineCount);
  152.     this->SetFrame(myExtent,true);
  153. }
  154.  
  155. //----------------------------------------------------------------------------------------
  156. // TLineView::Draw: 
  157. //----------------------------------------------------------------------------------------
  158. #pragma segment GVRes
  159. pascal void TLineView::Draw(const VRect& area)
  160. {
  161.     FontInfo    fi;
  162.     
  163.     TextFont(times);
  164.     TextSize(12);
  165.     TextFace(normal);
  166.     GetFontInfo(fi);
  167.     short lh = fi.ascent+fi.descent+fi.leading;
  168.     
  169.     ArrayIndex firstLine = area.top / lh + 1;
  170.     ArrayIndex lastLine = area.bottom / lh;
  171.     
  172.         
  173.     if(lastLine > fMyLineServer->GetLineCount())
  174.         lastLine = fMyLineServer->GetLineCount();
  175.     
  176.     CStr255    aLine;
  177.     CRect qdRect;
  178.     ViewToQDRect(area,qdRect);
  179.     short where = qdRect.top + fi.ascent;
  180.     
  181.     for(ArrayIndex i=firstLine;i<=lastLine;i++) {
  182.         fMyLineServer->GetALine(i, aLine);
  183.         MoveTo(0,where);
  184.         DrawString(aLine);
  185.         where+=lh;
  186.     }
  187. }
  188.  
  189. //------------------------------ •TListView• ---------------------------------
  190. // Basic TextListView class
  191. //----------------------------------------------------------------------------------------
  192. // TListView::IListView: 
  193. //----------------------------------------------------------------------------------------
  194. #pragma segment MAOpen
  195. pascal void TListView::IListView(TLineServer *aLineServer)
  196. {    
  197.     fMyLineServer = aLineServer;
  198.     ArrayIndex lineCount = fMyLineServer->GetLineCount();
  199.     
  200.     if(fNumOfRows)                    // clear the view to zippo
  201.         DelRowFirst(fNumOfRows);
  202.  
  203.     InsRowFirst((short)lineCount, 15);    // match the supply of data
  204. }
  205.  
  206. //----------------------------------------------------------------------------------------
  207. // TListView::GetItemText: 
  208. //----------------------------------------------------------------------------------------
  209. #pragma segment GVRes
  210. pascal void TListView::GetItemText(short item,
  211.                                        CStr255& aString)
  212. {
  213.     fMyLineServer->GetALine(item, aString);
  214. }
  215.  
  216.  
  217. //------------------------------ •TMatrixView• ---------------------------------
  218. // basic TextGridViewClass
  219. //----------------------------------------------------------------------------------------
  220. // TMatrixView::IMatrixView: 
  221. //----------------------------------------------------------------------------------------
  222. #pragma segment MAOpen
  223. pascal void TMatrixView::IMatrixView(TLineServer *aLineServer)
  224. {
  225.     fMyLineServer = aLineServer;
  226.     ArrayIndex lineCount = fMyLineServer->GetLineCount();
  227.     
  228.     short lines = (short)(lineCount/fNumOfCols);
  229.     if(lines > 1)
  230.         lines--;
  231.     InsRowFirst(lines, 15);    // match the supply of data
  232. }
  233.  
  234. //----------------------------------------------------------------------------------------
  235. // TMatrixView::GetText: 
  236. //----------------------------------------------------------------------------------------
  237. #pragma segment GVRes
  238. pascal void TMatrixView::GetText(GridCell item,
  239.                                        CStr255& aString)
  240. {
  241.     // This assumes that the data in the line server is arranged in row order, i.e.:
  242.     // row1,col1
  243.     // row1,col2
  244.     // row2,col1
  245.     // row2,col2 etc.
  246.     
  247.     ArrayIndex theOne = ((item.v - 1) * fNumOfCols) + item.h;
  248.     fMyLineServer->GetALine(theOne, aString);
  249. }
  250.